const {ccclass, property} = cc._decorator;

@ccclass
export default class DialogueComponent extends cc.Component {
    data:{list:{content:string}[],head:cc.SpriteFrame,end:any};
    sp:cc.SpriteFrame;

    
    onLoad() {
        
        var url = "head/2";
        
        cc.loader.loadRes(url, cc.SpriteFrame,  (err, spriteFrame) =>{
            this.showDialog({list:[{content:"h1"},{content:"h2"},{content:"h3"},{content:"h4"}],
            buttons:[{label:"功能1",click:()=>{console.log("调用了功能1");}},{label:"功能2",click:()=>{console.log("调用了功能2");}}],head:spriteFrame,end:()=>{}});
        });
        
        // init logic
    }

    //content 对话内容  head:说话的人的图片
    
    @property(cc.RichText)
    content:cc.RichText;
    @property(cc.Sprite)
    head:cc.Sprite;
    @property(cc.Layout)
    buttonsLayout:cc.Layout;
    @property(cc.Button)
    buttonTemplate:cc.Button;
    showDialog(data:any){
        this.buttonsLayout.node.active = false;
        this.data = data;
        this.head.spriteFrame = data.head;
        if(data.buttons!=null){
            //this.buttonsLayout.node.removeAllChildren();
            for(let i=0;i<data.buttons.length;i++){
                let btnInstance=cc.instantiate(this.buttonTemplate.node);
                btnInstance.active = true
                let lblText:cc.Label = btnInstance.getComponentInChildren(cc.Label)
                lblText.string = data.buttons[i].label
                btnInstance.on("click",data.buttons[i].click);
                this.buttonsLayout.node.addChild(btnInstance);
            }
           

           
        }
        this.nextContent();
        
    }

    nextContent(){
        let item = this.data.list.shift()
        if(this.data.list.length==0){
            this.buttonsLayout.node.active = true;
        }
        if(item!=null){
            this.content.string = item.content
        }else{
            this.closeCurrent();
            if(this.data.end!=null){
                this.data.end();
            }
        }
        
       
    }

    accessTask(){
        
        console.log("接受任务")
        this.closeCurrent()
    }

    closeCurrent(){
        let s:string ="";
        this.node.active = false;
    }
}

